package com.qztc.hire_end_core.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qztc.hire_end_core.mapper.ForumTypeMapper;
import com.qztc.hire_end_dto.dto.ForumType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@CrossOrigin
@RequestMapping("/forumType")
@Tag(name = "论坛类型管理接口")
public class ForumTypeController {
    @Autowired
    private ForumTypeMapper forumTypeMapper;

    @Operation(summary = "查询所有论坛类型")
    @GetMapping("/getAll")
    public List<ForumType> getAll() {
        return new ForumType().selectAll();
    }

    @Operation(summary = "根据forumTypeId获取具体论坛类型")
    @GetMapping("/getbyforumTypeId")
    public ForumType getbyforumTypeId(@RequestParam Integer forumTypeId) {
        QueryWrapper<ForumType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("forum_typeid", forumTypeId);
        return new ForumType().selectOne(queryWrapper);
    }

    @Operation(summary = "根据forumTypeId删除某一个具体论坛类型")
    @DeleteMapping("/delete")
    public boolean delete(@RequestBody ForumType forumType) {
        return forumType.deleteById(forumType.getForumTypeId());
    }

    @Operation(summary = "根据forumTypeId修改某一个具体论坛类型")
    @PutMapping("/update")
    public boolean update(@RequestBody ForumType forumType) {
        QueryWrapper<ForumType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("forum_typename", forumType.getForumTypeName());
        if(forumType.selectCount(queryWrapper) > 0) {
            return false;
        }
        return forumType.updateById();
    }

    @Operation(summary = "增加论坛类型")
    @PostMapping("/add")
    public boolean add(@RequestBody ForumType forumType) {
        QueryWrapper<ForumType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("forum_typename", forumType.getForumTypeName());
        if(forumType.selectCount(queryWrapper) > 0) {
            return false;
        }
        return forumType.insert();
    }

    @Operation(summary = "批量删除论坛类型")
    @DeleteMapping("/deleteMany")
    public boolean deleteMany(@RequestBody List<Integer> forumTypeIds) {
        if (forumTypeIds == null || forumTypeIds.isEmpty()) {
            return false;
        }
        QueryWrapper<ForumType> wrapper = new QueryWrapper<>();
        wrapper.in("forum_typeid", forumTypeIds);
        return new ForumType().delete(wrapper);
    }

    @Operation(summary = "批量增加论坛类型")
    @PostMapping("/addMany")
    public boolean addMany(@RequestBody List<ForumType> forumTypes) {
        if (forumTypes == null || forumTypes.isEmpty()) {
            return false;
        }
        // 批量重复校验
        QueryWrapper<ForumType> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("forum_typename",
                forumTypes.stream().map(ForumType::getForumTypeName).collect(Collectors.toList()));
        if (forumTypeMapper.selectCount(queryWrapper) > 0) {
            return false;
        }
        // 批量插入SQL
        String sql = "insert into forum_type (forum_typeid, forum_typename) values" +
                forumTypes.stream()
                        .map(type -> String.format("(%d,'%s')", type.getForumTypeId(), type.getForumTypeName()))
                        .collect(Collectors.joining(","));
        return forumTypeMapper.executeSql(sql);
    }
}